package com.tangdi.production.mpaychl.service.impl.toffpay;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.tangdi.production.mpaychl.base.service.BaseTranService;
import com.tangdi.production.mpaychl.service.impl.bncp.util.MD5Util;
import com.tangdi.production.mpbase.constants.MsgCT;
import com.tangdi.production.mpbase.exception.TranException;
import com.tangdi.production.mpbase.util.MoneyUtils;
import com.tangdi.production.mpbase.util.ObjectUtil;

@Service("Toffpay_service")
public class ToffpayServiceImpl implements BaseTranService {
	private static final Logger logger = LoggerFactory.getLogger(ToffpayServiceImpl.class);
	
	private static final String URL = "http://www.toffpay.com/pay";
	
	private static String UID = "8086";				//

	private static String NOTIFY_URL = "http://ce.imbitpay.com:8083/mpcctp/bncp/notify-url.json";

	public static String TOKEN = "6ebd2dd46f1b45eda040e43dd1e65e0d";
	
	@Override
	public Map<String, Object> execute(Map<String, Object> param) throws TranException {
		logger.info("进入BNCP_serviceV2处理接口,参数:[{}]", param);
		try {
			String uid = UID;
			String payAmt = ObjectUtil.getString(param.get("payAmt"));
			logger.info("支付金额[{}分]", payAmt);
			payAmt = MoneyUtils.toStrYuan(payAmt);
			logger.info("支付金额转换为元[{}]", payAmt);
			String price = payAmt;
			String paytype = "1";	// 支付宝
			String notify_url = NOTIFY_URL;
			String return_url = "" + param.get("frontUrl");
			String user_order_no = "" + param.get("prdordno");
			
			String encodeStr = UID.concat(price)
					.concat(paytype)
					.concat(notify_url)
					.concat(return_url)
					.concat(user_order_no)
					.concat(TOKEN);
			
			String sign = MD5Util.encode(encodeStr.getBytes());
			
			StringBuilder returtnHtml = new StringBuilder();
			returtnHtml.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\" /></head><body><script language=\"javascript\">window.onload=function(){document.payfrm.submit();}</script>");
			returtnHtml.append("<form id=\"payfrm\" name=\"payfrm\" action=\"").append(URL).append("\" method=\"post\" >");
			returtnHtml.append("<input type=\"hidden\" name=\"uid\" value=\"").append(uid).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"price\" value=\"").append(price).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"paytype\" value=\"").append(paytype).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"notify_url\" value=\"").append(notify_url).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"return_url\" value=\"").append(return_url).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"user_order_no\" value=\"").append(user_order_no).append("\" />");
			returtnHtml.append("<input type=\"hidden\" name=\"sign\" value=\"").append(sign).append("\" />");
			returtnHtml.append("</form></body></html>");
			
			Map<String,Object> backMap = new HashMap<>();
			backMap.put("ordStatus", MsgCT.ORDSTATUS_PROCESSING);
			backMap.put("contentType", MsgCT.DATA_TYPE_HTML);
			backMap.put("busContent", returtnHtml.toString());
			return backMap;
		} catch (Exception e) {
			logger.error("渠道BNCP-V2调用异常:{}", e.getMessage(), e);
			Map<String,Object> backMap = new HashMap<>();
			backMap.put("ordStatus", MsgCT.ORDSTATUS_FAIL);
			backMap.put("msg", "渠道BNCP-V2调用异常:" + e.getMessage());
			return backMap;
		}
	}
	
	public static void main(String[] args) throws TranException {
		Map<String, Object> params = new HashMap<>();
		params.put("payAmt", "16");
		params.put("frontUrl", "http://58.220.30.99:8081/qrcode-starter/returnTest");
		params.put("prdordno", System.currentTimeMillis() + RandomStringUtils.randomAlphabetic(5));
		
		Map<String, Object> returnMap = new ToffpayServiceImpl().execute(params);
		
		System.err.println(returnMap.toString());
		
		System.out.println();
		System.out.println(returnMap.get("busContent"));
	}
	
}
